<template>
  <div class="goods-list">
    <div class="goods-item" v-for="value in goodsList" :key="value.id" @click="getInfo(value.id)">
      <img :src="value.img_url" alt>
      <h1>{{ value.title }}</h1>
      <div class="info">
        <p class="price">
          <span class="now">¥&nbsp;{{ value.sell_price }}</span>
          <span class="old">¥&nbsp;{{ value.market_price }}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{ value.stock_quantity }}件</span>
        </p>
      </div>
    </div>
    <mt-button size="large" type="danger" @click="getMore">加载更多</mt-button>
  </div>
</template>
<script>
export default {
  data(){
    return {
      pageIndex:1,
      goodsList:[]
    }
  },
  created(){
    this.getGoodsList()
  },
  methods:{
    getGoodsList(){
      this.$http.get('api/getgoods?pageindex='+this.pageIndex++).then(res=>{
        console.log(res);
        if(res.body.status==0){
          this.goodsList = this.goodsList.concat(res.body.message)
        }
      })
    },
    getMore(){
      this.getGoodsList()
    },
    // 不通过标签的形式实现路由跳转,通过js控制路由的形式来跳转
    getInfo(id){
      // $route 里面是一些路由的参数
      // $router 里面是一些路由导航的方法 push()  back() forwards() go()

      // this.$router.push('/home/goodsInfo/'+id)
      // this.$router.push({path:'/home/goodsInfo/'+id})
      //
      // 这个name指向路由router.js中路由规则里的name
      this.$router.push({name:'goodsInfo',params:{id}})

      // 如果是通过查询字符串传参,不可以用name,用的是 path 指向路由规则中的name
      
      // 通过路径传参,params属性和name属性搭配使用
    }
  }
};
</script>
<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-flow: row wrap;
  padding: 7px;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    // background: red;
    min-height: 293px;
    padding: 0 4px;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    display: flex;
    flex-flow: column nowrap;
    justify-content: space-between;
    img {
      width: 100%;
    }
    h1 {
      font-size: 14px;
    }
    .info {
      background: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
      }
    }
  }
}
</style>


